package com.watson.minio.springminio.file.controller;


import com.watson.minio.springminio.file.service.FileManageService;
import com.watson.minio.springminio.file.util.Result;
import io.swagger.annotations.*;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * @Description:
 * @Author: fan
 * @Data: 2021/5/8 15:26
 * @Version: 1.0v
 */
@Api(tags = "File流实现文件上传")
@RestController
@RequestMapping("file")
public class FileUtilController {

    @Resource
    private FileManageService fileManageService;

    @PostMapping(value = "/uploadFile", consumes = MediaType.MULTIPART_FORM_DATA_VALUE, headers = "content-type=multipart/form-data")
    @ApiOperation(value = "上传本地文件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "文件", dataType = "__file", required = true),
            @ApiImplicitParam(name = "module", value = "文件分类", paramType = "query", dataType = "String", required = true)
    })
    @CrossOrigin(allowCredentials = "true", allowedHeaders = "*")
    public Result<String> fileUpload(MultipartFile file, String module) {
        try {
            return new Result<String>().ok(fileManageService.fileUpload(file, module));
        } catch (Exception e) {
            e.printStackTrace();
            return new Result<String>().error(e.getMessage());
        }
    }

    @PostMapping(value = "/uploadFileByUrl")
    @ApiOperation(value = "上传文件的网络地址")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "contextPath", value = "文件地址", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "module", value = "文件分类", paramType = "query", dataType = "String", required = true)
    })
    @CrossOrigin(allowCredentials = "true", allowedHeaders = "*")
    public Result<String> fileUploadByUrl(String contextPath, String module) {
        try {
            return new Result<String>().ok(fileManageService.fileUpload(contextPath, module));
        } catch (Exception e) {
            e.printStackTrace();
            return new Result<String>().error("地址无法解析，上传文件失败");
        }
    }

    @PostMapping(value = "/fileUploadByBase64")
    @ApiOperation(value = "上传文件的base64编码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "base64", value = "文件的base64", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "module", value = "文件分类", paramType = "query", dataType = "String", required = true)
    })
    @CrossOrigin(allowCredentials = "true", allowedHeaders = "*")
    public Result<String> fileUploadByBase64(String base64, String module) {

        try {
            return new Result<String>().ok(fileManageService.fileUploadByBase64(base64, module));
        } catch (Exception e) {
            e.printStackTrace();
            return new Result<String>().error("上传文件失败");
        }
    }

    @PostMapping(value = "/uploadFileBatch", consumes = MediaType.MULTIPART_FORM_DATA_VALUE, headers = "content-type=multipart/form-data")
    @ApiOperation(value = "批量上传本地文件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "files", value = "文件", allowMultiple = true, dataType = "__file", required = true),
            @ApiImplicitParam(name = "module", value = "文件分类", paramType = "query", dataType = "String", required = true)
    })
    @CrossOrigin(allowCredentials = "true", allowedHeaders = "*")
    public Result<List<String>> fileUploadBatch(MultipartFile[] files, String module) {
        try {
            return new Result<List<String>>().ok(fileManageService.fileUploadBatch(files, module));
        } catch (Exception e) {
            e.printStackTrace();
            return new Result().error(e.getMessage());
        }
    }

    public static void main(String[] args) {

        String regex =  "^/api/(.*)$";
        String s = "/api/file/uploadFileBatch?module=11";
        Pattern compile = Pattern.compile(regex);

        Matcher matcher = compile.matcher(s);
        matcher.replaceAll("/");

        String s1 = s.replaceAll("^/api/(.*)$", "/$");
        System.out.println(s.matches("^/api/(.*)$"));
        System.out.println(s1);
    }

}
